<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Surplus_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_surplus';
	}

        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),array(
						'name' => 'customerID',
						'label' => $this->lang->line('customerID'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'surplus',
						'label' => $this->lang->line('surplus'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'note',
						'label' => $this->lang->line('note'),
						'width' => '20%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name'  => 'button',
						'width' => '5%',
						'sort'  => FALSE,
						'label' =>  $right['add']==TRUE?'<div class="btn-group">
						<a style="width: 82px;" href="'.site_url('auth/surplus/index/add').'" class="btn btn btn-success">'.$this->lang->line('create').'</a>
						</div>':"",
						'searchoptions' => false
				)
		);
	}
	
	public function json_data($controller, $right){
		$this->datatables
		->select("tbl.id,tbl.customerID,tbl.surplusRemaine,tbl.note,tbl.lastupdated,bc.name")
		->from($this->table_name.' AS tbl')
                ->join('book_contacts AS bc','bc.customerID = tbl.customerID');
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                
		foreach($datatables['aaData'] as $item){
			$ouput['aaData'][] = array(
					$item['id'],
                                        $item['customerID'],
                                        $item['name'],
                                        $this->bookinglib->my_number_format($item['surplusRemaine'],2, ',', ','),
                                        $item['note'],
                                        date("d-m-Y H:i:s",strtotime($item['lastupdated'])),
                                        $this->add_button($controller, $right, $item)
			);
		}
	
		return json_encode($ouput);
	}

}